package net.dlcode.dylan.context;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.util.StringUtils;

public class DylanContext extends AbstractXmlApplicationContext{
	private static final String DEFAULT_CONFIGURATIONLOCATION = "classpath:dylan.xml";
	
	private String[] configurationLocation;
	
	public DylanContext() {
		this(DEFAULT_CONFIGURATIONLOCATION, true);
	}
	
	public DylanContext(String configurationLocation,boolean refresh){
		this.configurationLocation = StringUtils.delimitedListToStringArray(configurationLocation, CONFIG_LOCATION_DELIMITERS);
		if(refresh)
			refresh();
	}
	
	public <T> T getBean(Class<T> requiredType){
		return requiredType.cast(BeanFactoryUtils.beanOfTypeIncludingAncestors(getBeanFactory(), requiredType));
	}
	
	@Override
	protected String[] getConfigLocations() {
		return this.configurationLocation;
	}
}
